import Vue from "vue";
import { getAcount, logout , getUserPlayList ,getLikeIdList , getPersonalFm ,fmTrash} from "@/api/request";
import router from "@/router/index";
export default {
  // 获取登录状态
  async getAcount({ commit, dispatch }) {
    const { data: res } = await getAcount();
    if (res.code !== 200) return;
    if (res.profile !== null) {
      commit("setLoginInfo", res);
      commit("setIsLogin", true);
      dispatch('getMyPlayList');
      dispatch('getLikeList');
    } else {
      commit("setLoginInfo", { account: {}, profile: {} });
      commit("setIsLogin", false);
      Vue.prototype.$notify({
        title: "登录体验完整功能",
        type: "warning",
        dangerouslyUseHTMLString: true,
        message: `<section>部分功能需要登录后才能使用，如每日推荐等，点击头像可以登录,建议使用验证码登录</section>
                <br/>
                <section>点击访问<a style="margin:0 10px" href="https://gitee.com/luabu/vue-music" target="_blank">Gitee</a><a href="https://www.zclstrive.top/" target="_blank">老张杂货铺</a></section>
                `,
        duration: 0,
        position: "top-left",
      });
    }
  },
  // 退出登录
  logout({ commit, state }) {
    if (!state.isLogin) return Vue.prototype.$message.error("似乎没有登录");
    Vue.prototype
      .$confirm("将要退出登录，是否继续", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(async () => {
        const { data: res } = await logout();
        if (res.code !== 200) return;
        Vue.prototype.$message.success("退出成功");
        commit("setLoginInfo", { account: null, profile: null });
        commit("setIsLogin", false);
        router.push("/");
      })
      .catch((err) => {
        console.log(err);
        Vue.prototype.$message({
          type: "info",
          message: "已取消",
        });
      });
  },
  // 获取喜欢的音乐列表
  async getLikeList({commit , state}){
   const {data:res} = await getLikeIdList(state.profile.userId);
   if(res.code !== 200) return;
   if(res.ids instanceof Array){
    commit('setLikeIdList',{type:"get",data:res.ids});
   }
  },
  // 获取个人歌单
  async getMyPlayList({commit,state}){
    if(!state.isLogin) return;
    const {data:res} = await getUserPlayList(state.profile.userId);
    if(res.code !== 200) return;
    commit('setMyPlayList',res.playlist)
  },
  //获取播放音乐列表
  playMusic({commit},payload){
      //当前音乐列表
      commit('setMusicList',payload.list);
      //当前播放音乐的id
      commit('setCurrenMusicId',payload.id);
      //当前音乐在音乐列表的index
      let index = payload.list.findIndex((item) => item.id == payload.id);
      commit('setCurrenIndex',index);
      //播放的type
      commit('setPlayType','music');
      //是否播放
      commit('setPlayState',true)
  },
  //获取私人fm
  async getPersonalFm({commit , state , dispatch},operate){
    if(operate.type === 'get'){
      const {data:res} = await getPersonalFm();
      console.log(res);
      if(res.code !== 200) return Vue.prototype.$message.error('获取私人FM失败');
      const list = [];
      res.data.forEach((item) => {
        list.push({
          id : item.id,
          name : item.name,
          fee : item.fee,
          alia : item.alias,
          ar : item.artists,
          al : item.album,
          dt : item.duration,
          mv : item.mvid
        })
      })
      commit('setPersonalFm',{type : 'set' ,data:list});
      commit('setPlayType','personalFm');
      commit('setPlayState',true);
    } else if(operate.type === 'next') {
      if(state.musicList.length - 1 === state.currenIndex){
        dispatch('getPersonalFm',{type : 'get'});
      } else{
        commit('setPersonalFm',{type : 'next'});
      }
    } else if(operate.type === 'remove'){
      const {data:res} = await fmTrash(operate.id);
      if(res.code !== 200) return Vue.prototype.$message.error('操作失败');
      Vue.prototype.$message.success('成功移动到垃圾桶');
      dispatch('getPersonalFm',{type : 'next'});
    }
  }
};
